package cn.kwq.Security.expression;

import cn.kwq.Security.domain.LoginUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 自定义权限处理器
 */
@Component("mySecurity")
public class SGExpressionRoot {

    public boolean hasAuthority(String authority){
        //在SecurityContextHolder中获取当前用户的权限
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        List<String> permissions = loginUser.getPermissions();
        //判断用户权限集合中是否存在传入的authority
        return permissions.contains(authority);
    }
}
